#!/bin/sh
########################################################################
# \
export LD_LIBRARY_PATH="./lib:$LD_LIBRARY_PATH"
# \
export PATH="./bin:$PATH"
# \
exec wish8.6 -file $0 ${1+"$@"}
# \
exit
########################################################################
# HammerDB
# Copyright (C) 2003-2023 Steve Shaw
# Author contact information at: http://www.hammerdb.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with this program; If not, see <https://www.gnu.org/licenses/>
########################################################################
global hdb_version
set hdb_version "v4.9"
set mainGeometry +10+10
set UserDefaultDir [ file dirname [ info script ] ]
::tcl::tm::path add "$UserDefaultDir/modules"

namespace eval autostart {
    set autostartap "false"
    if {$argc == 0} { ; } else {
    if {$argc != 2 || [lindex $argv 0] != "auto" } {
puts {Usage: hammerdb [ auto [ script_to_autoload.tcl  ] ]}
exit
	} else {
        set autostartap "true"
        set autoloadscript [lindex $argv 1]
if { [ file exists $autoloadscript ] && [ file isfile $autoloadscript ] && [ file extension $autoloadscript ] eq ".tcl" } {
;# autostart selected and tcl file exists
	     } else {
puts {Usage: hammerdb [ auto [ script_to_autoload.tcl  ] ]}
exit
		}
        } 
   }
}

namespace eval LoadingProgressMeter {
    set max 14

    wm title            . "Loading HammerDB"
    wm protocol         . WM_DELETE_WINDOW {#Do nothing}
    wm overrideredirect . 1
    wm geometry         . +100+120
    wm transient	.
    tk appname "HammerDB Splash Screen"

      set logo_file logo.tcl
if [catch {source [ file join $UserDefaultDir images $logo_file ]}] {
                puts stderr "While loading image file\
                        \"$logo_file\"...\n$errorInfo"
        }
    #Display LOGO in SVG format to scale HD displays, fallback is PNG
    if {[catch {package require tksvg}]} {
    # SVG not supported use original PNG logo
    set logoset [ create_logo_images ]
    set logo [ dict get $logoset logo ]
    set logim [ image create photo -height 120 -width 694 -data $logo ]
    } else {
    # SVG supported use SVG logo to scale
    set logoset [ create_logo_images ]
    set logosetsvg [ create_logo_images_svg ]
    set logosvg [ dict get $logosetsvg logosvg ]
    if { [ format "%.2f" [ tk scaling ] ] <= 1.33 } {
    set logim [ image create photo -data $logosvg -format "svg -scaletoheight 240" ]
    	} else {
	set scaleheight [ expr {round((240/1.333333)*[tk scaling])} ]
    set logim [ image create photo -data $logosvg -format "svg -scaletoheight $scaleheight" ]
	}
    }
    # Window Icon is not supported in SVG always use PNG
    set iconwht [ dict get $logoset iconwht ]
    set iconw [ image create photo -height 120 -width 97 -data $iconwht ]
    foreach imagedata {logo iconwht logoset logosetsvg} {unset -nocomplain $imagedata}
    #Splash Screen Fonts
    set gbg white;
    set mid   {TkDefaultFont 20 {bold}} 
    set load  {TkDefaultFont 14}
    if {$tcl_platform(platform) == "windows"} {
    if {{Segoe UI} in [ font families ] } {
    set mid   {{Segoe UI} 20 {bold}} 
    set load  {{Segoe UI} 14}
        } else {
    set mid   {TkDefaultFont 20 {bold}} 
    set load  {TkDefaultFont 14}
        }
    } else {
    if {{Liberation Sans} in [ font families ] } {
    set mid   {{Liberation Sans} 20 {bold}} 
    set load  {{Liberation Sans} 14}
        } else {
    set mid   {TkDefaultFont 20 {bold}} 
    set load  {TkDefaultFont 14}
        }
    }
    . conf  -bg $gbg  -cursor watch

    wm iconphoto . -default $iconw
    frame .progress -bg $gbg
  label .title -text " $hdb_version "  -font $mid \
            -bg $gbg  -fg #f99317  -padx 0
    label .progress.loadmsg  -textvariable loadtext \
            -anchor s  -bg $gbg  -font $load
    if { [ format "%.2f" [ tk scaling ] ] <= 1.33 } {
    set disp_canv [ canvas .progress.canv -highlightthickness 0 -bg $gbg -height 150 -width 694 ]	
    $disp_canv create image 350 70 -image $logim
    	} else {
	set disp_canv [ canvas .progress.canv -highlightthickness 0 -bg $gbg -height [expr {round((150/1.333333)*[tk scaling])}] -width [expr {round((694/1.333333)*[tk scaling])}] ]
	$disp_canv create image [expr {round((350/1.333333)*[tk scaling])} ] [expr {round((70/1.333333)*[tk scaling])} ] -image $logim
	}

    foreach imagedata {logim iconw} {unset -nocomplain $imagedata}

	pack $disp_canv 
	pack .progress.loadmsg

    scale .progress.bar  -from 0  -to 1  -label {}  -bd 0 \
            -orient horizontal  -length 20  -showvalue 0 \
            -background #003068  -troughcolor $gbg  -state normal \
            -tickinterval 0  -width [expr {round((10/1.333333)*[tk scaling])}] -takefocus 0  -cursor {} \
            -relief flat  -sliderrelief flat  -sliderlength 4
    .progress.bar set 0

    bindtags .progress.bar {. all}
    pack .progress.bar -fill x -expand 1

    grid .title   -          -padx 1.5m -pady 1m -sticky ew
    if [winfo exist .evalmsg] {grid .evalmsg  -columnspan 3}
    grid .progress  -sticky nsew

    variable count -1
    variable len 0
    proc updateprogress {args} {
        global loadtext
        variable count
        variable max

        incr count
        set width [winfo width .progress.bar]
        .progress.bar conf -sliderlength \
                [expr {int(($width-4)*$count/$max)+4}]
        if {$count%5 == 0} {
            update; 
        } else {
            update idletasks
        }
    }
    trace variable ::loadtext w [namespace code updateprogress]
    set ::loadtext ""

    namespace export check_progress_length
    proc check_progress_length {} {
        variable count
        variable max
        variable len
        if {$count!=$max} {
            puts stderr "[namespace current]::max not correctly\
                    adjusted - FIX to be [expr {$count-$len}]"
        }
        catch {unset loadtext}
    }
}
namespace import LoadingProgressMeter::*

if [info exist env(Load_List)] {
    foreach {sofile description} $env(Load_List) {
        set loadtext "Loading Object Code: $description"
        if [catch {load $sofile} s] {
            puts stderr "Failed to load $sofile\nPerhaps\
                    it should be built?\n$s"
            exit 1
        }
    }
}

append modulelist { Thread msgcat tablelist_tile tooltip tkcon xml xscale ctext comm emu_graph socktest tkblt http huddle jobs tkpath }

set loadtext "Loading hammerdb modules"
after 100
for { set modcount 0 } { $modcount < [llength $modulelist] } { incr modcount } {
    set m [lindex $modulelist $modcount]
		set loadtext $m
	if [catch { package require $m }] {
                puts stderr "While loading module\
                        \"$m\"...\n$errorInfo"
                exit 1
        }
    }

append iconlist { icons.tcl }

set loadtext "Loading hammerdb icons"
after 100
for { set iconcount 0 } { $iconcount < [llength $iconlist] } { incr iconcount } {
    set f [lindex $iconlist $iconcount]
		set loadtext $f
	if [catch {source [ file join $UserDefaultDir images $f ]}] {
                puts stderr "While loading component file\
                        \"$f\"...\n$errorInfo"
                exit 1
        }
    }

append loadlist { gentheme.tcl genvu.tcl gentpcc.tcl gentpch.tcl gengen.tcl genmodes.tcl gentab.tcl gencnv.tcl gentccmn.tcl gentc.tcl genmetrics.tcl gened.tcl genxml.tcl geninit.tcl }

set loadtext "Loading generic source"
after 100
for { set loadcount 0 } { $loadcount < [llength $loadlist] } { incr loadcount } {
    set f [lindex $loadlist $loadcount]
		set loadtext $f
	if [catch {source [ file join $UserDefaultDir src generic $f ]}] {
                puts stderr "While loading component file\
                        \"$f\"...\n$errorInfo"
                exit 1
        }
    }

set loadtext "Loading database source"
after 100
for { set dbsrccount 0 } { $dbsrccount < [llength $dbsrclist] } { incr dbsrccount } {
    set f [lindex $dbsrclist $dbsrccount]
		set loadtext $f
	if [catch {source [ file join $UserDefaultDir src $f ]}] {
                puts stderr "Error loading database source files/$f"
        }
    }

after 100
set loadtext "Starting HammerDB"
update
#pause to display splash screen
after 2000
wm withdraw .
wm deiconify .ed_mainFrame
ed_edit
if { $autostart::autostartap == "true" } {
    ed_file_load
    start_autopilot
	}
tkwait window .ed_mainFrame
exit
